/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.xy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xy.entity.TimeStageEntity;
import com.xy.vo.TimeStageVO;
import com.xy.excel.TimeStageExcel;
import com.xy.mapper.TimeStageMapper;
import com.xy.service.ITimeStageService;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 入场时间段 服务实现类
 */
@Service
public class TimeStageServiceImpl extends BaseServiceImpl<TimeStageMapper, TimeStageEntity> implements ITimeStageService {

	@Override
	public IPage<TimeStageVO> selectTimeStagePage(IPage<TimeStageVO> page, TimeStageVO timeStage) {
		return page.setRecords(baseMapper.selectTimeStagePage(page, timeStage));
	}


	@Override
	public List<TimeStageExcel> exportTimeStage(Wrapper<TimeStageEntity> queryWrapper) {
		List<TimeStageExcel> timeStageList = baseMapper.exportTimeStage(queryWrapper);
		//timeStageList.forEach(timeStage -> {
		//	timeStage.setTypeName(DictCache.getValue(DictEnum.YES_NO, TimeStage.getType()));
		//});
		return timeStageList;
	}

	@Override
	public void deleteTimeStageEntity(Long viewerId) {
		LambdaQueryWrapper<TimeStageEntity> lambdaQueryWrapper = Wrappers.lambdaQuery();
		lambdaQueryWrapper.eq(TimeStageEntity::getViewerId, viewerId);
		lambdaQueryWrapper.eq(TimeStageEntity::getDataType, 0L);
		List<TimeStageEntity> admissionTimeEntities = this.list(lambdaQueryWrapper);
		if(CollectionUtil.isNotEmpty(admissionTimeEntities)){
			List<Long> list = admissionTimeEntities.stream().map(TimeStageEntity::getId).collect(Collectors.toList());
			this.removeBatchByIds(list);
		}
	}

	@Override
	public void saveTimeStageEntity(Date startTime, Date endTime, Long viewerId) {
		if(null != startTime &&  null != endTime){
			TimeStageEntity time = BeanUtil.newInstance(TimeStageEntity.class);
			time.setDataType(0);
			time.setStartTime(startTime);
			time.setEndTime(DateUtil.parse(DateUtil.format(endTime,DateUtil.PATTERN_DATE)+" 23:59:59",DateUtil.PATTERN_DATETIME));
			time.setViewerId(viewerId);
			this.save(time);
		}
	}
	/**
	 * 时间段查询
	 */
	@Override
	public List<TimeStageEntity> queryTimeStageEntity(Long viewerId) {
		LambdaQueryWrapper<TimeStageEntity> TimeStageEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
		TimeStageEntityLambdaQueryWrapper.eq(TimeStageEntity::getViewerId, viewerId);
		List<TimeStageEntity> timeEntityList = this.list(TimeStageEntityLambdaQueryWrapper);
		return timeEntityList;
	}


	@Override
	public List<TimeStageEntity> getTimeStage(Long viewerId){
		LambdaQueryWrapper<TimeStageEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(TimeStageEntity::getViewerId, viewerId);
		List<TimeStageEntity> timeEntities = this.list(wrapper);
		return timeEntities;
	}


}
